<?php
class M_laporan extends CI_Model {
	
	function getlist($filter,$start,$end){
		$sql = "SELECT anggaran_id,
				(SELECT count(*) FROM m_anggaran as count_parent where anggaran_parent_id = a.anggaran_id and 
				anggaran_aktif = 'y'
				) as child,
				anggaran_sat1,anggaran_sat2,anggaran_sat3,anggaran_sat4,
				anggaran_vol1,anggaran_vol2,anggaran_vol3,anggaran_vol4,anggaran_kegiatan,
				CONCAT(anggaran_kd1,'.',anggaran_kd2, '.',anggaran_kd3,'.',anggaran_kd4,'.',anggaran_kd5,'.', anggaran_kd6)
				as kode,
			concat( repeat('--',anggaran_level-1),anggaran_kegiatan)as kegiatan
			,TRIM(
				CONCAT(
					IF(anggaran_vol1 IS NULL,'' ,anggaran_vol1),' ',anggaran_sat1, ' ',
					IF(anggaran_vol2 IS NULL,'' ,anggaran_vol2),' ',anggaran_sat2,' ',
					IF(anggaran_vol3 IS NULL,'' ,anggaran_vol3),' ', anggaran_sat3)
			) as satuan,
			anggaran_value,
			anggaran_value * (
				IF(anggaran_vol1 IS NULL, 1,anggaran_vol1) *
				IF(anggaran_vol2 IS NULL, 1,anggaran_vol2) *
				IF(anggaran_vol3 IS NULL, 1,anggaran_vol3) 
			) as anggaran_total, IF (anggaran_level = 1,
				(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a1 WHERE 
					a1.anggaran_kd1 = a.anggaran_kd1 ) 
				,
					IF (anggaran_level = 2,
				(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a2 WHERE 
							a2.anggaran_kd1 = a.anggaran_kd1  AND 
							a2.anggaran_kd2 = a.anggaran_kd2 ) 
						,
							IF (anggaran_level = 3,
									(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a3 WHERE 
									a3.anggaran_kd1 = a.anggaran_kd1  AND  
									a3.anggaran_kd2 = a.anggaran_kd2 AND 
									a3.anggaran_kd3 = a.anggaran_kd3 ) 
								,
								IF (anggaran_level = 4,
										(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a4 WHERE  
										a4.anggaran_kd1 = a.anggaran_kd1  AND  
										a4.anggaran_kd2 = a.anggaran_kd2 AND 
										a4.anggaran_kd3 = a.anggaran_kd3 AND 
										a4.anggaran_kd4 = a.anggaran_kd4 ) 
									,0
								)
							)
					)
			) as anggaran_jumlah
			FROM m_anggaran a
			WHERE anggaran_kd1 is not null AND anggaran_aktif = 'y'";
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(anggaran_kegiatan) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		$sql .= " ORDER BY anggaran_kd1, anggaran_kd2, anggaran_kd3, anggaran_kd4, anggaran_kd5, anggaran_kd6";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		foreach($query->result() as $q){
			$arr[] = $q; 
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function cetak($tahun){
		$sql = "SELECT anggaran_id,anggaran_level,
				(SELECT count(*) FROM m_anggaran as count_parent where anggaran_parent_id = a.anggaran_id and 
				anggaran_aktif = 'y'
				) as child,
				anggaran_sat1,anggaran_sat2,anggaran_sat3,anggaran_sat4,
				anggaran_vol1,anggaran_vol2,anggaran_vol3,anggaran_vol4,anggaran_kegiatan,
				CONCAT(anggaran_kd1,'.',anggaran_kd2, '.',anggaran_kd3,'.',anggaran_kd4,'.',anggaran_kd5,'.', anggaran_kd6)
				as kode,
			concat( repeat('--',anggaran_level-1),anggaran_kegiatan)as kegiatan
			,TRIM(
				CONCAT(
					IF(anggaran_vol1 IS NULL,'' ,anggaran_vol1),' ',anggaran_sat1, ' ',
					IF(anggaran_vol2 IS NULL,'' ,anggaran_vol2),' ',anggaran_sat2,' ',
					IF(anggaran_vol3 IS NULL,'' ,anggaran_vol3),' ', anggaran_sat3)
			) as satuan,
			anggaran_value,
			anggaran_value * (
				IF(anggaran_vol1 IS NULL, 1,anggaran_vol1) *
				IF(anggaran_vol2 IS NULL, 1,anggaran_vol2) *
				IF(anggaran_vol3 IS NULL, 1,anggaran_vol3) 
			) as anggaran_total, IF (anggaran_level = 1,
				(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a1 WHERE 
					a1.anggaran_kd1 = a.anggaran_kd1 ) 
				,
					IF (anggaran_level = 2,
				(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a2 WHERE 
							a2.anggaran_kd1 = a.anggaran_kd1  AND 
							a2.anggaran_kd2 = a.anggaran_kd2 ) 
						,
							IF (anggaran_level = 3,
									(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a3 WHERE 
									a3.anggaran_kd1 = a.anggaran_kd1  AND  
									a3.anggaran_kd2 = a.anggaran_kd2 AND 
									a3.anggaran_kd3 = a.anggaran_kd3 ) 
								,
								IF (anggaran_level = 4,
										(SELECT SUM(anggaran_value * 
			IF (anggaran_vol1 IS NULL, 1, anggaran_vol1) *
			IF (anggaran_vol2 IS NULL, 1, anggaran_vol2) *
			IF (anggaran_vol3 IS NULL, 1, anggaran_vol3) *
			IF (anggaran_vol4 IS NULL, 1, anggaran_vol4) 
			) FROM m_anggaran a4 WHERE  
										a4.anggaran_kd1 = a.anggaran_kd1  AND  
										a4.anggaran_kd2 = a.anggaran_kd2 AND 
										a4.anggaran_kd3 = a.anggaran_kd3 AND 
										a4.anggaran_kd4 = a.anggaran_kd4 ) 
									,0
								)
							)
					)
			) as anggaran_jumlah ";
			
			for($i =1;$i<=12;$i++){
			
			$sql .= ", 
			IF (anggaran_level = 1,
				(SELECT SUM(pengajuandet_value) FROM t_pengajuandet p1 LEFT JOIN m_anggaran aa1
				ON m_anggaran_id = anggaran_id
				LEFT JOIN t_pengajuan q1
				ON t_pengajuan_id = pengajuan_id
				WHERE 
					MONTH(q1.pengajuan_date) = ".$i." AND 
					YEAR(q1.pengajuan_date) = ".$tahun." AND 
					aa1.anggaran_kd1 = a.anggaran_kd1),
					IF (anggaran_level = 2,
						 (SELECT SUM(pengajuandet_value) FROM t_pengajuandet p2 LEFT JOIN m_anggaran aa2
							ON m_anggaran_id = anggaran_id
							LEFT JOIN t_pengajuan q2
							ON t_pengajuan_id = pengajuan_id
							WHERE 
							MONTH(q2.pengajuan_date) = ".$i." AND 
							YEAR(q2.pengajuan_date) = ".$tahun." AND 
							aa2.anggaran_kd1 = a.anggaran_kd1 AND
							aa2.anggaran_kd2 = a.anggaran_kd2),
							IF (anggaran_level = 3,
								 (SELECT SUM(pengajuandet_value) FROM t_pengajuandet p3 LEFT JOIN m_anggaran aa3
									ON m_anggaran_id = anggaran_id
									LEFT JOIN t_pengajuan q3
									ON t_pengajuan_id = pengajuan_id
									WHERE 
									MONTH(q3.pengajuan_date) = ".$i." AND 
									YEAR(q3.pengajuan_date) = ".$tahun." AND 
									aa3.anggaran_kd1 = a.anggaran_kd1 AND
									aa3.anggaran_kd2 = a.anggaran_kd2 AND
									aa3.anggaran_kd3 = a.anggaran_kd3 ),
									IF (anggaran_level = 4,
										 (SELECT SUM(pengajuandet_value) FROM t_pengajuandet p4 LEFT JOIN m_anggaran aa4
											ON m_anggaran_id = anggaran_id
											LEFT JOIN t_pengajuan q4
											ON t_pengajuan_id = pengajuan_id
											WHERE 
											MONTH(q4.pengajuan_date) = ".$i." AND 
											YEAR(q4.pengajuan_date) = ".$tahun." AND 
											aa4.anggaran_kd1 = a.anggaran_kd1 AND
											aa4.anggaran_kd2 = a.anggaran_kd2 AND
											aa4.anggaran_kd3 = a.anggaran_kd3 AND
											aa4.anggaran_kd4 = a.anggaran_kd4 ),
											IF (anggaran_level = 5,
												 (SELECT SUM(pengajuandet_value) FROM t_pengajuandet p5 LEFT JOIN m_anggaran aa5
													ON m_anggaran_id = anggaran_id
													LEFT JOIN t_pengajuan q5
													ON t_pengajuan_id = pengajuan_id
													WHERE 
													MONTH(q5.pengajuan_date) = ".$i." AND 
													YEAR(q5.pengajuan_date) = ".$tahun." AND 
													aa5.anggaran_kd1 = a.anggaran_kd1 AND
													aa5.anggaran_kd2 = a.anggaran_kd2 AND
													aa5.anggaran_kd3 = a.anggaran_kd3 AND
													aa5.anggaran_kd4 = a.anggaran_kd4 AND
													aa5.anggaran_kd5 = a.anggaran_kd5 ),
														 (SELECT SUM(pengajuandet_value) FROM t_pengajuandet p6 LEFT JOIN m_anggaran aa6
															ON m_anggaran_id = anggaran_id
															LEFT JOIN t_pengajuan q6
															ON t_pengajuan_id = pengajuan_id
															WHERE 
															MONTH(q6.pengajuan_date) = ".$i." AND 
															YEAR(q6.pengajuan_date) = ".$tahun." AND 
															aa6.anggaran_kd1 = a.anggaran_kd1 AND
															aa6.anggaran_kd2 = a.anggaran_kd2 AND
															aa6.anggaran_kd3 = a.anggaran_kd3 AND
															aa6.anggaran_kd4 = a.anggaran_kd4 AND
															aa6.anggaran_kd5 = a.anggaran_kd5 AND
															aa6.anggaran_kd6 = a.anggaran_kd6 )
													)
											)
									)	
						)
			) as pengajuan_umk".$i." ,
			IF (anggaran_level = 1,
				 (SELECT sum(kwitansi_value) FROM
					m_anggaran aaa1
					LEFT JOIN t_pengajuandet
					on m_anggaran_id = anggaran_id
					LEFT JOIN t_kwitansi
					on t_pengajuandet_id = pengajuandet_id
					WHERE 
					MONTH(kwitansi_date) = ".$i." AND 
					YEAR(kwitansi_date) = ".$tahun." AND 
					aaa1.anggaran_kd1 = a.anggaran_kd1),
					IF (anggaran_level = 2,
						 (SELECT sum(kwitansi_value) FROM
							m_anggaran aaa2
							LEFT JOIN t_pengajuandet
							on m_anggaran_id = anggaran_id
							LEFT JOIN t_kwitansi
							on t_pengajuandet_id = pengajuandet_id
							WHERE 
							MONTH(kwitansi_date) = ".$i." AND 
							YEAR(kwitansi_date) = ".$tahun." AND 
							aaa2.anggaran_kd1 = a.anggaran_kd1 AND 
							aaa2.anggaran_kd2 = a.anggaran_kd2),
							IF (anggaran_level = 3,
								 (SELECT sum(kwitansi_value) FROM
									m_anggaran aaa3
									LEFT JOIN t_pengajuandet
									on m_anggaran_id = anggaran_id
									LEFT JOIN t_kwitansi
									on t_pengajuandet_id = pengajuandet_id
									WHERE 
									MONTH(kwitansi_date) = ".$i." AND 
									YEAR(kwitansi_date) = ".$tahun." AND 
									aaa3.anggaran_kd1 = a.anggaran_kd1 AND 
									aaa3.anggaran_kd2 = a.anggaran_kd2 AND
									aaa3.anggaran_kd3 = a.anggaran_kd3),
									IF (anggaran_level = 4,
										 (SELECT sum(kwitansi_value) FROM
											m_anggaran aaa4
											LEFT JOIN t_pengajuandet
											on m_anggaran_id = anggaran_id
											LEFT JOIN t_kwitansi
											on t_pengajuandet_id = pengajuandet_id
											WHERE 
											MONTH(kwitansi_date) = ".$i." AND 
											YEAR(kwitansi_date) = ".$tahun." AND 
											aaa4.anggaran_kd1 = a.anggaran_kd1 AND 
											aaa4.anggaran_kd2 = a.anggaran_kd2 AND
											aaa4.anggaran_kd3 = a.anggaran_kd3 AND
											aaa4.anggaran_kd4 = a.anggaran_kd4),
											IF (anggaran_level = 5,
												 (SELECT sum(kwitansi_value) FROM
													m_anggaran aaa5
													LEFT JOIN t_pengajuandet
													on m_anggaran_id = anggaran_id
													LEFT JOIN t_kwitansi
													on t_pengajuandet_id = pengajuandet_id
													WHERE 
													MONTH(kwitansi_date) = ".$i." AND 
													YEAR(kwitansi_date) = ".$tahun." AND 
													aaa5.anggaran_kd1 = a.anggaran_kd1 AND 
													aaa5.anggaran_kd2 = a.anggaran_kd2 AND
													aaa5.anggaran_kd3 = a.anggaran_kd3 AND
													aaa5.anggaran_kd4 = a.anggaran_kd4 AND
													aaa5.anggaran_kd5 = a.anggaran_kd5),
													(SELECT sum(kwitansi_value) FROM
														m_anggaran aaa6
														LEFT JOIN t_pengajuandet
														on m_anggaran_id = anggaran_id
														LEFT JOIN t_kwitansi
														on t_pengajuandet_id = pengajuandet_id
														WHERE 
														MONTH(kwitansi_date) = ".$i." AND 
														YEAR(kwitansi_date) = ".$tahun." AND 
														aaa6.anggaran_kd1 = a.anggaran_kd1 AND 
														aaa6.anggaran_kd2 = a.anggaran_kd2 AND
														aaa6.anggaran_kd3 = a.anggaran_kd3 AND
														aaa6.anggaran_kd4 = a.anggaran_kd4 AND
														aaa6.anggaran_kd5 = a.anggaran_kd5 AND
														aaa6.anggaran_kd6 = a.anggaran_kd6)
													)
											)
									)
							)
			) as realisasi".$i.",
			IF(
				anggaran_level = 1, ( SELECT SUM(anggaran_value *(

					IF(
						anggaran_vol1 IS NULL,
						1,
						anggaran_vol1
					)*
					IF(
						anggaran_vol2 IS NULL,
						1,
						anggaran_vol2
					)*
					IF(
						anggaran_vol3 IS NULL,
						1,
						anggaran_vol3
					) *
					IF(
						anggaran_vol4 IS NULL,
						1,
						anggaran_vol4
					)
				) - IFNULL(
				 (SELECT SUM(kwitansi_value) FROM m_anggaran pp1 LEFT JOIN t_pengajuandet ON m_anggaran_id = anggaran_id LEFT JOIN t_kwitansi ON t_pengajuandet_id = pengajuandet_id WHERE
						MONTH(kwitansi_date)= ".$i."
					AND YEAR(kwitansi_date)= ".$tahun."
					AND pp1.anggaran_kd1 = a.anggaran_kd1
					AND pp1.anggaran_kd2 = a.anggaran_kd2
					AND pp1.anggaran_kd3 = a.anggaran_kd3
					AND pp1.anggaran_kd4 = a.anggaran_kd4
					AND pp1.anggaran_kd5 = a.anggaran_kd5
					AND pp1.anggaran_kd6 = a.anggaran_kd6),0))
					FROM m_anggaran sp
					WHERE 
					sp.anggaran_kd1 = a.anggaran_kd1)
				,

			IF(
				anggaran_level = 2, ( SELECT SUM(anggaran_value *(

					IF(
						anggaran_vol1 IS NULL,
						1,
						anggaran_vol1
					)*
					IF(
						anggaran_vol2 IS NULL,
						1,
						anggaran_vol2
					)*
					IF(
						anggaran_vol3 IS NULL,
						1,
						anggaran_vol3
					) *
					IF(
						anggaran_vol4 IS NULL,
						1,
						anggaran_vol4
					)
				) - IFNULL(
				 (SELECT SUM(kwitansi_value) FROM m_anggaran pp2 LEFT JOIN t_pengajuandet ON m_anggaran_id = anggaran_id LEFT JOIN t_kwitansi ON t_pengajuandet_id = pengajuandet_id WHERE
						MONTH(kwitansi_date)= ".$i."
					AND YEAR(kwitansi_date)= ".$tahun."
					AND pp2.anggaran_kd1 = a.anggaran_kd1
					AND pp2.anggaran_kd2 = a.anggaran_kd2
					AND pp2.anggaran_kd3 = a.anggaran_kd3
					AND pp2.anggaran_kd4 = a.anggaran_kd4
					AND pp2.anggaran_kd5 = a.anggaran_kd5
					AND pp2.anggaran_kd6 = a.anggaran_kd6),0))
					FROM m_anggaran sp2 
					WHERE 
					sp2.anggaran_kd1 = a.anggaran_kd1
					AND sp2.anggaran_kd2 = a.anggaran_kd2)
				,

			IF(
				anggaran_level = 3 , ( SELECT SUM(anggaran_value *(

					IF(
						anggaran_vol1 IS NULL,
						1,
						anggaran_vol1
					)*
					IF(
						anggaran_vol2 IS NULL,
						1,
						anggaran_vol2
					)*
					IF(
						anggaran_vol3 IS NULL,
						1,
						anggaran_vol3
					) *
					IF(
						anggaran_vol4 IS NULL,
						1,
						anggaran_vol4
					)
				) - IFNULL(
				 (SELECT SUM(kwitansi_value) FROM m_anggaran pp3 LEFT JOIN t_pengajuandet ON m_anggaran_id = anggaran_id LEFT JOIN t_kwitansi ON t_pengajuandet_id = pengajuandet_id WHERE
						MONTH(kwitansi_date)= ".$i."
					AND YEAR(kwitansi_date)= ".$tahun."
					AND pp3.anggaran_kd1 = a.anggaran_kd1
					AND pp3.anggaran_kd2 = a.anggaran_kd2
					AND pp3.anggaran_kd3 = a.anggaran_kd3
					AND pp3.anggaran_kd4 = a.anggaran_kd4
					AND pp3.anggaran_kd5 = a.anggaran_kd5
					AND pp3.anggaran_kd6 = a.anggaran_kd6),0))
					FROM m_anggaran sp3 
					WHERE 
					sp3.anggaran_kd1 = a.anggaran_kd1
					AND sp3.anggaran_kd2 = a.anggaran_kd2
					AND sp3.anggaran_kd3 = a.anggaran_kd3),
				IF(anggaran_level = 4 , ( SELECT SUM(anggaran_value *(

					IF(
						anggaran_vol1 IS NULL,
						1,
						anggaran_vol1
					)*
					IF(
						anggaran_vol2 IS NULL,
						1,
						anggaran_vol2
					)*
					IF(
						anggaran_vol3 IS NULL,
						1,
						anggaran_vol3
					)*
					IF(
						anggaran_vol4 IS NULL,
						1,
						anggaran_vol4
					)
				) - IFNULL(
				 (SELECT SUM(kwitansi_value) FROM m_anggaran pp4 LEFT JOIN t_pengajuandet ON m_anggaran_id = anggaran_id LEFT JOIN t_kwitansi ON t_pengajuandet_id = pengajuandet_id WHERE
						MONTH(kwitansi_date)= ".$i."
					AND YEAR(kwitansi_date)= ".$tahun."
					AND pp4.anggaran_kd1 = a.anggaran_kd1
					AND pp4.anggaran_kd2 = a.anggaran_kd2
					AND pp4.anggaran_kd3 = a.anggaran_kd3
					AND pp4.anggaran_kd4 = a.anggaran_kd4
					AND pp4.anggaran_kd5 = a.anggaran_kd5
					AND pp4.anggaran_kd6 = a.anggaran_kd6),0))
					FROM m_anggaran sp4 
					WHERE 
					sp4.anggaran_kd1 = a.anggaran_kd1
					AND sp4.anggaran_kd2 = a.anggaran_kd2
					AND sp4.anggaran_kd3 = a.anggaran_kd3
					AND sp4.anggaran_kd4 = a.anggaran_kd4),( SELECT anggaran_value *(

					IF(
						anggaran_vol1 IS NULL,
						1,
						anggaran_vol1
					)*
					IF(
						anggaran_vol2 IS NULL,
						1,
						anggaran_vol2
					)*
					IF(
						anggaran_vol3 IS NULL,
						1,
						anggaran_vol3
					)
				) - IFNULL(
				 (SELECT SUM(kwitansi_value) FROM m_anggaran pp6 LEFT JOIN t_pengajuandet ON m_anggaran_id = anggaran_id LEFT JOIN t_kwitansi ON t_pengajuandet_id = pengajuandet_id WHERE
						MONTH(kwitansi_date)= ".$i."
					AND YEAR(kwitansi_date)= ".$tahun."
					AND pp6.anggaran_kd1 = a.anggaran_kd1
					AND pp6.anggaran_kd2 = a.anggaran_kd2
					AND pp6.anggaran_kd3 = a.anggaran_kd3
					AND pp6.anggaran_kd4 = a.anggaran_kd4
					AND pp6.anggaran_kd5 = a.anggaran_kd5
					AND pp6.anggaran_kd6 = a.anggaran_kd6),0)
					FROM m_anggaran sp1 
					WHERE 
					sp1.anggaran_kd1 = a.anggaran_kd1
					AND sp1.anggaran_kd2 = a.anggaran_kd2
					AND sp1.anggaran_kd3 = a.anggaran_kd3
					AND sp1.anggaran_kd4 = a.anggaran_kd4
					AND sp1.anggaran_kd5 = a.anggaran_kd5
					AND sp1.anggaran_kd6 = a.anggaran_kd6))
			)
			)
			)as sisa_pagu".$i."
			";
			}
			$sql .= "FROM m_anggaran a
			WHERE anggaran_kd1 is not null AND anggaran_aktif = 'y' AND anggaran_tahun = ".$tahun."";
		$sql .= " ORDER BY anggaran_kd1, anggaran_kd2, anggaran_kd3, anggaran_kd4, anggaran_kd5, anggaran_kd6";
		return $query = $this->db->query($sql)->result();
	}	
}